package org.example;

import org.example.Model.ServiceMetaInfo;
import org.example.config.RPCConfig;
import org.example.config.RegistryConfig;
import org.example.registry.IRegistry;
import org.example.registry.Registry;
import org.example.registry.RegistryFactory;
import org.example.server.HttpServer;
import org.example.server.VertxHttpServer;
import org.example.service.UserService;

public class Provider {
    public static void main(String[] args) {
        // RPC框架初始化
        RPCApplication.init();

        // 注册服务
        // register the service
        String serviceName = UserService.class.getName();
        Registry.register(serviceName, UserServiceImpl.class);

        // 注册服务到注册中心
        RPCConfig rpcConfig = RPCApplication.getConfig();
        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
        registryConfig.setRegistry("redis");
        IRegistry registry = RegistryFactory.getInstance(registryConfig.getRegistry());
        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
        serviceMetaInfo.setServiceName(serviceName);
        serviceMetaInfo.setServiceHost("localhost");
        serviceMetaInfo.setServicePort(8080);
        try {registry.register(serviceMetaInfo);} catch(Exception e) {throw new RuntimeException(e);}

        // Start the HTTP server
        HttpServer httpServer = new VertxHttpServer();
        httpServer.doStart(rpcConfig.getServerPort());
    }
}